class ItemStyle < ActiveRecord::Base
  validates_presence_of :name
  validates_uniqueness_of :name

  attr_accessible :name, :status, :flag, :meta_keywords, :meta_description, :description
  
  STATUSES = { 0 => '默认', 2 => '首页推荐', -1 => '删除' }
  FLAGS = { 0 => '风格', 1 => '潮讯' }
  
  named_scope :valid, :conditions => ['status >= ?', 0]
  
  named_scope :filter, lambda { |params|
    c, h = [], {}
    
    unless params[:id].blank?
      c << "id LIKE :id"
      h[:id] = "%#{params[:id]}%"
    end
    unless params[:name].blank?
      c << "name LIKE :name"
      h[:name] = "%#{params[:name]}%"
    end
    
    { :conditions => [c.join(' AND '), h] }
  }
  
  class << self
  
    def flag_options
      FLAGS.inject([]){ |arr, obj| arr << obj.reverse }
    end
    
    def status_options
      STATUSES.inject([]){ |arr, obj| arr << obj.reverse }
    end
  end
  
  def to_s
    name
  end
  
  def display_flag
    FLAGS[flag]
  end
  
  def display_status
    STATUSES[status]
  end
  
  def del
    update_attributes!(:status => -1)
  end
end
